app.controller('DocumentListCtrl', ['$scope','$state','$rootScope','$stateParams','$modal','$timeout','toaster','$filter', function($scope,$state,$rootScope,$stateParams,$modal,$timeout,toaster,$filter) {
    var tree;
    $scope.selectGroupId = $stateParams.selectGroupId;
    $scope.startIndex=0;
    $scope.batchSize=25;
    $scope.totalCount = 0;
    $scope.folder;
    $scope.isLoad=false;
    $scope.isFavorite=false;
    $scope.documentsAndFolders;
    $scope.folders_tree = tree = {};
    $scope.myFolders_tree = tree = {};
    $scope.folders;
    $scope.myFolders;
    $scope.timestamp = 0;
    $scope.defaultSelected = function() {
        if($stateParams.folder!=-1){
            $scope.folder = $stateParams.folder;
            setTimeout(function(){
                $scope.myFolders_tree.select_branch_id($scope.folder);
            },0)
        } else{
            setTimeout(function(){
				if($scope.myFolders[0]!=null){
					$scope.myFolders_tree.select_branch_id($scope.myFolders[0].id);
				}
            },0)
        }
    }
    requestServlet("getDocumentFolderTree",{},function(rs){
        $scope.folders = rs.documentsFolders;
        $scope.myFolders = rs.myDocumentsFolders;
        $scope.defaultSelected();
    },function(msg){});
    $scope.getDocumentsByFolder = function() {
        $scope.isCreateKC = false;
        $scope.isCreateFolder = false;
        requestServlet("getDocumentsAndFolders",{folderId: $scope.folder,isFavorite:$scope.isFavorite,
            searchValue: $scope.searchValue,
            startIndex: $scope.startIndex,
            batchSize: $scope.batchSize},function(rs){
            $scope.totalCount = rs.totalCount;
            if ($scope.startIndex == 0) {
                $scope.documentsAndFolders = rs.list;
            } else {
                $scope.documentsAndFolders = $scope.documentsAndFolders.concat(rs.list);
            }
            if (rs.list.length < $scope.batchSize) {
                $scope.isLoad = true;
            }
        },function(msg){});

    }
    $scope.getDocumentsByFolder();
    $scope.my_folder_handler_branch;
    $scope.my_folder_handler = function(branch) {
        $scope.isFavorite=false;
        $scope.searchValue=null;
        $scope.startIndex=0;
        $scope.isLoad=false;
        if($scope.my_folder_handler_branch==branch){
            $scope.my_folder_handler_branch.selected=false;
            $scope.my_folder_handler_branch = null;
            $scope.folder=null;
        }else {
            $scope.my_folder_handler_branch = branch;
            if ($scope.folder_handler_branch != null) {
                $scope.folder_handler_branch.selected = false;
                $scope.folder_handler_branch = null;
            }
            $scope.folder = branch.id;
        }
        $scope.getDocumentsByFolder();
    };
    $scope.folder_handler_branch;
    $scope.folder_handler = function(branch) {
        $scope.isFavorite=false;
        $scope.searchValue=null;
        $scope.startIndex=0;
        $scope.isLoad=false;
        if($scope.folder_handler_branch==branch){
            $scope.folder_handler_branch.selected=false;
            $scope.folder_handler_branch = null;
            $scope.folder=null;
        }else {
            $scope.folder_handler_branch = branch;
            if ($scope.my_folder_handler_branch != null) {
                $scope.my_folder_handler_branch.selected = false;
                $scope.my_folder_handler_branch = null;
            }
            $scope.folder = branch.id;
        }
        $scope.getDocumentsByFolder();
    };
    $scope.clean_folder_handler = function() {
        $scope.startIndex=0;
        $scope.isLoad=false;
        if($scope.folder_handler_branch!=null){
            $scope.folder_handler_branch.selected=false;
            $scope.folder_handler_branch = null;
        }
        if($scope.my_folder_handler_branch!=null){
            $scope.my_folder_handler_branch.selected=false;
            $scope.my_folder_handler_branch = null;
        }
        //$scope.folders_tree.collapse_all()
        //$scope.myFolders_tree.collapse_all()
        $scope.folder = null;
        $scope.getDocumentsByFolder();
    };
    $scope.set_folder = function(folder) {
        $scope.isFavorite=false;
        if(folder==null){
            $scope.clean_folder_handler();
        }else{
            $scope.searchValue=null;
            $scope.folder = folder;
            $scope.folders_tree.select_branch_id(folder);
            $scope.myFolders_tree.select_branch_id(folder);
        }

    };

    $scope.searchDocumentsAndFolders= function(e){
        if(e==null){
            $scope.startIndex=0;
            $scope.isLoad=false;
            $scope.clean_folder_handler();
            $scope.getDocumentsByFolder();
        }else{
            var keycode = window.event?e.keyCode:e.which;
            if(keycode==13){
                $scope.startIndex=0;
                $scope.isLoad=false;
                $scope.clean_folder_handler();
                $scope.getDocumentsByFolder();
            }
        }
    }
    $scope.scrollEvent= function(){
        $scope.startIndex = $scope.startIndex + $scope.batchSize;
        $scope.getDocumentsByFolder();
    }
    $scope.favoriteDocuments= function(){
        if($scope.isFavorite){
            $scope.isFavorite = false;
        }else{
            $scope.isFavorite= true;
            $scope.searchValue=null;
            $scope.startIndex=0;
            $scope.isLoad=false;
        }
        $scope.clean_folder_handler();
    }
    //收藏切换
    $scope.setFavorite = function(id,favorite,index){
        if(favorite){
            favorite = 1;
            $scope.documentsAndFolders[index].favorite = false;
        }else{
            $scope.documentsAndFolders[index].favorite = true;
            favorite = 0;
        }
        requestServlet("setContentFavorite",{is_favorite:favorite,id:id},function(rs){
        },function(msg){});
    }

    $scope.documentsView = function(id){
        var documentIds=[];
        var documentId;
        var viewIndex =0;
        var folderNum = 0;
        for(var i=0;i<$scope.documentsAndFolders.length;i++){
            if($scope.documentsAndFolders[i].type==1){
                documentIds.push($scope.documentsAndFolders[i].id);
                if($scope.documentsAndFolders[i].id==id){
                    documentId =  $scope.documentsAndFolders[i].id;
                    viewIndex = i-folderNum;
                }
            }else{
                folderNum++;
            }
        }
        var modalInstance = $modal.open({
            templateUrl: 'tpl/documents_view_modal.html',
            controller: 'documentsViewModalCtrl',
            size: "docView",
            resolve: {
                type: function () {return "operation"},
                documentIds: function () {return documentIds},
                scope: function () {return $scope},
                viewIndex: function () {return viewIndex},
                documentId:function () {return documentId}
            }
        });
        modalInstance.result.then(function (rs) {
            if(rs=="delete"){
                $scope.startIndex=0;
                $scope.isLoad=false;
                $scope.getDocumentsByFolder();
            }else if(rs=="move"){
                requestServlet("getDocumentFolderTree",{},function(rs){
                    $scope.folders = rs.documentsFolders;
                    $scope.myFolders = rs.myDocumentsFolders;
                    setTimeout(function(){
                        $scope.folders_tree.select_branch_id($scope.folder);
                        $scope.myFolders_tree.select_branch_id($scope.folder);
                        if ($scope.folder_handler_branch != null) {
                            $scope.folders_tree.expand_branch($scope.folder_handler_branch)
                        }
                        if ($scope.my_folder_handler_branch != null) {
                            $scope.myFolders_tree.expand_branch($scope.my_folder_handler_branch)
                        }
                    })
                },function(msg){});
            }
        }, function () {
            //dismiss
        });
    }

    $scope.isCreateKC = false;
    $scope.isCreateFolder = false;
    $scope.createKCName;
    $scope.createFolderName;
    $scope.submitCreateContent = function(name){
        requestServlet("createContent",{name:name,parent:$scope.folder,isKCPublic:$("#isKCPublic").val()},function(rs){
            if($scope.folder==null){
                $scope.totalCount = $scope.totalCount+1;
                if($("#isKCPublic").val()=="public"){
                    $scope.folders_tree.add_root_branch(rs);
                }else{
                    $scope.myFolders_tree.add_root_branch(rs);
                }
                $scope.getDocumentsByFolder();
            }else {
                if($scope.folder_handler_branch!=null){
                    $scope.folders_tree.add_branch($scope.folder_handler_branch,rs)
                }
                if($scope.my_folder_handler_branch!=null){
                    $scope.myFolders_tree.add_branch($scope.my_folder_handler_branch,rs)
                }
                $scope.getDocumentsByFolder();
            }
            $scope.isCreateKC = false;
            $scope.isCreateFolder = false;
            $scope.createKCName="";
            $scope.createFolderName="";
        },function(msg){});
    }

    $scope.selectedFile = function (thiss){
        $scope.$apply(function($scope) {
            toaster.pop("wait","common.prompt","common.documentUpLoadLoading",2000,null,function(){});
        });
        var flag = $(thiss).attr("flag");
        uploadFileById(thiss,function(rs){
            $scope.$apply(function($scope) {
                toaster.pop("success","common.prompt","common.documentSuccess",null,null,function(){});
                $scope.getDocumentsByFolder();
            });
        })
    }
    $scope.deleteContent = function (id){
        $scope.openPromptModal("common.prompt","common.ifDelete",function(){
            requestServlet("deleteContent",{id:id},function(){
                if($scope.folder==null) {
                    $scope.folders_tree.remove_branch_id($scope.folder, id)
                    $scope.myFolders_tree.remove_branch_id($scope.folder,id)
                    $scope.totalCount = $scope.totalCount - 1;
                    $scope.startIndex=0;
                    $scope.isLoad=false;
                    $scope.getDocumentsByFolder();
                }else {
                    if ($scope.folder_handler_branch != null) {
                        $scope.folders_tree.remove_branch_id($scope.folder_handler_branch, id)
                    }
                    if ($scope.my_folder_handler_branch != null) {
                        $scope.myFolders_tree.remove_branch_id($scope.my_folder_handler_branch,id)
                    }
                    $scope.startIndex=0;
                    $scope.isLoad=false;
                    $scope.getDocumentsByFolder();
                }

            },function(msg){});
        });
    }
    $scope.moveDocument = function (id,type,name){
        var modalInstance = $modal.open({
            templateUrl: 'tpl/select_documents_modal.html',
            controller: 'selectDocumentsModalCtrl',
            size: "selectDoc",
            resolve: {
                type:function(){ return "move"; },
                documentName:function(){ return name; }
            }
        });
        modalInstance.result.then(function (parentIds) {
            requestServlet("updateContent",{id:id,type:type,name:name,parentId:parentIds[0]},function(){
                requestServlet("getDocumentFolderTree",{},function(rs){
                    $scope.folders = rs.documentsFolders;
                    $scope.myFolders = rs.myDocumentsFolders;
                    setTimeout(function(){
                        $scope.folders_tree.select_branch_id($scope.folder);
                        $scope.myFolders_tree.select_branch_id($scope.folder);
                        if ($scope.folder_handler_branch != null) {
                            $scope.folders_tree.expand_branch($scope.folder_handler_branch)
                        }
                        if ($scope.my_folder_handler_branch != null) {
                            $scope.myFolders_tree.expand_branch($scope.my_folder_handler_branch)
                        }
                    })
                },function(msg){});
            },function(msg){});

        }, function () {
            //dismiss
        });
    }

    $scope.submitUpdateContent = function (index,id,type,name){
        requestServlet("updateContent",{id:id,type:type,name:name},function(){
            $scope.documentsAndFolders[index].label = name;
            $scope.documentsAndFolders[index].isEdit = false;
            if($scope.folder==null) {
                $scope.folders_tree.update_branch_id($scope.folder_handler_branch, id,name)
                $scope.myFolders_tree.update_branch_id($scope.my_folder_handler_branch,id,name)
            }
            if ($scope.folder_handler_branch != null) {
                $scope.folders_tree.update_branch_id($scope.folder_handler_branch, id,name)
            }
            if ($scope.my_folder_handler_branch != null) {
                $scope.myFolders_tree.update_branch_id($scope.my_folder_handler_branch,id,name)
            }
            $scope.getDocumentsByFolder();
        },function(msg){});

    }
    $scope.isAllCheck = false;



}]);


